package com.skillworld.webapp.web.pages.rest.school;

import java.util.List;
import java.util.Locale;

import org.apache.tapestry5.annotations.ContentType;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;

import com.skillworld.webapp.model.courseservice.CourseService;
import com.skillworld.webapp.model.enrollment.Enrollment;
import com.skillworld.webapp.model.util.Pagination;
import com.skillworld.webapp.web.util.ErrorMessages;
import com.skillworld.webapp.web.util.Parameter;
import com.skillworld.webapp.web.util.UserSession;


@ContentType("text/xml")
public class SearchEnrollments
{
    private static final int DEFAULT_PAGE_NUMBER = 1;
    private static final int DEFAULT_PAGE_SIZE = 20;
     
    @SuppressWarnings("unused")
    @Property
    private String errorMessage = null;
    
    @SuppressWarnings("unused")
    @Property
    private List<Enrollment> enrollments;
    
    @Property
    private Pagination pagination;
    
    @SuppressWarnings("unused")
    @Property
    private Enrollment e;   // loop iterator
    
    @Inject
    private CourseService courseService;

    @SessionState(create=false)
    private UserSession userSession;
    
    private boolean userSessionExists;
    
    @Inject
    private Request request;
    

    void onPassivate()
    {
        return;
    }
    
    void onActivate()
    {
        if (!userSessionExists)
        {
            errorMessage = ErrorMessages.NOT_LOGGED_IN;
            return;
        }
        
        String query = request.getParameter("q");
        String deptParam = request.getParameter("dept");
        String langParam = request.getParameter("lang");
        String finishedParam = request.getParameter("finished");
        String pageParam = request.getParameter("page");
        String psizeParam = request.getParameter("psize");
        
        if (query == null)
        {
            errorMessage = ErrorMessages.INVALID_ARGUMENTS;
            return;
        }
        
        try 
        {           
            Long dept = Parameter.maybeLong(deptParam);
            Locale lang = (langParam != null)
                              ? new Locale(langParam)
                              : null;
            Boolean finished = Parameter.maybeBoolean(finishedParam);
            int page = Parameter.maybeInt(pageParam, DEFAULT_PAGE_NUMBER);
            int pageSize = Parameter.maybeInt(psizeParam, DEFAULT_PAGE_SIZE);
         
            int numberOfEnrollments = 
                    courseService.numberOfEnrollments(query, 
                                                    userSession.getUserId(),
                                                    dept, lang, finished);
            pagination = new Pagination(numberOfEnrollments, pageSize, page);
         
            if (pageSize <= 0 || !pagination.isValidPage())
            {
                errorMessage = ErrorMessages.INVALID_ARGUMENTS;
                return;
            }
            
            enrollments = courseService.searchEnrollments(query,
                                            userSession.getUserId(),
                                            dept, lang,
                                            finished, 
                                            pagination.getStartIndex(),
                                            pagination.getCount());                                            
        }
        catch (IllegalArgumentException e)
        { errorMessage = ErrorMessages.INVALID_ARGUMENTS; }
    }
}
